<?php
/**
 *
 */
namespace app\manage\controller\level;

use app\manage\controller\Base;

class Handle extends Base
{
    private $params;
    public function __construct(){
        parent::__construct();
        // token验证
        if(false == Base::checkToken()){
            $this->error('token error!');
        }
        // 参数去token
        $params = $this -> request -> param();
        unset($params['token']);
        $this->params = $params;
    }

    public function add(){
        $params = $this->params;
        $result = $this->validate($params,'Level.manage_add');
        if(true!==$result){
            $this->error($result);
        }
        $data = [
            'level_name' => $params['level_name'],
            'amount' => $params['amount'],
            'score' => $params['score'],
        ];
        $query = new \app\common\query\Level;
        if($query->saveOneData($data)){
            // 更新缓存
            $CACHE = \app\common\cache\CacheData::getInstance('Level');
            $CACHE -> updateCache();
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }

    public function edit(){
        $params = $this->params;
        $result = $this->validate($params,'Level.manage_edit');
        if(true!==$result){
            $this->error($result);
        }
        $data = [
            'level_name' => $params['level_name'],
            'score' => $params['score'],
        ];
        $query = new \app\common\query\Level;
        if($query->saveOneData($data,['id'=>$params['id']])){
            // 更新缓存
            $CACHE = \app\common\cache\CacheData::getInstance('Level');
            $CACHE -> updateCache();
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    public function del(){
        $this->error('暂时停止删除');
        $params = $this->params;
        $result = $this->validate($params,'Level.manage_del');
        if(true!==$result){
            $this->error($result);
        }
        $query = new \app\common\query\Level;
        if(false===$query->deleteOneData($params['id'])){
            $this->error($query->getError());
        }else{
            // 更新缓存
            $CACHE = \app\common\cache\CacheData::getInstance('Level');
            $CACHE -> updateCache();
            $this->success('删除成功');
        }
    }

}